package com.yuluo.aihelper.ai;

import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiHelperServiceFactory {
    //LangChain4j自带
    @Resource
    private ChatModel myQwenChatModel;
    //已在RagConfig注册为bean
    @Resource
    private ContentRetriever contentRetriever;
    @Resource
    private McpToolProvider mcpToolProvider;
    @Resource
    private StreamingChatModel qwenStreamingChatModel;


    @Bean
    public AiHelperService aiHelperService(){
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        return AiServices.builder(AiHelperService.class)
                .chatModel(myQwenChatModel) // 聊天模型
                .streamingChatModel(qwenStreamingChatModel)// 流式实时输出
                .chatMemory(chatMemory) // 会话记忆
                .chatMemoryProvider(memoryId ->
                        MessageWindowChatMemory.withMaxMessages(10))// 每个会话独立存储
                .contentRetriever(contentRetriever) // RAG
                .toolProvider(mcpToolProvider)// MCP工具
                .build();
    }
}
